package com.dayu.store.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dayu.common.utils.DateUtils;
import com.dayu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.store.mapper.AppletClerkUserMapper;
import com.dayu.store.domain.AppletClerkUser;
import com.dayu.store.service.IAppletClerkUserService;

/**
 * 店员用户Service业务层处理
 * 
 * @author HK
 * @date 2022-01-07
 */
@Service
public class AppletClerkUserServiceImpl implements IAppletClerkUserService 
{
    @Autowired
    private AppletClerkUserMapper appletClerkUserMapper;

    /**
     * 查询店员用户
     * 
     * @param id 店员用户ID
     * @return 店员用户
     */
    @Override
    public AppletClerkUser selectAppletClerkUserById(Long id)
    {
        return appletClerkUserMapper.selectAppletClerkUserById(id);
    }

    /**
     * 查询店员用户列表
     * 
     * @param appletClerkUser 店员用户
     * @return 店员用户
     */
    @Override
    public List<AppletClerkUser> selectAppletClerkUserList(AppletClerkUser appletClerkUser)
    {
        return appletClerkUserMapper.selectAppletClerkUserList(appletClerkUser);
    }

    /**
     * 新增店员用户
     * 
     * @param appletClerkUser 店员用户
     * @return 结果
     */
    @Override
    public int insertAppletClerkUser(AppletClerkUser appletClerkUser)
    {
        checkParams(appletClerkUser);
        appletClerkUser.setCreateTime(DateUtils.getNowDate());
        return appletClerkUserMapper.insertAppletClerkUser(appletClerkUser);
    }

    /**
     * 修改店员用户
     * 
     * @param appletClerkUser 店员用户
     * @return 结果
     */
    @Override
    public int updateAppletClerkUser(AppletClerkUser appletClerkUser)
    {
        checkParams(appletClerkUser);
        return appletClerkUserMapper.updateAppletClerkUser(appletClerkUser);
    }

    /**
     * 批量删除店员用户
     * 
     * @param ids 需要删除的店员用户ID
     * @return 结果
     */
    @Override
    public int deleteAppletClerkUserByIds(Long[] ids)
    {
        return appletClerkUserMapper.deleteAppletClerkUserByIds(ids);
    }

    /**
     * 删除店员用户信息
     * 
     * @param id 店员用户ID
     * @return 结果
     */
    @Override
    public int deleteAppletClerkUserById(Long id)
    {
        return appletClerkUserMapper.deleteAppletClerkUserById(id);
    }

    /**
     * 参数校验
     */
    private void checkParams(AppletClerkUser clerkUser) {
        if(StringUtils.isEmpty(clerkUser.getPhone())) {
            throw new RuntimeException("手机号不能为空！");
        }
        AppletClerkUser selUser = new AppletClerkUser();
        selUser.setPhone(clerkUser.getPhone());
        List<AppletClerkUser> selList = selectAppletClerkUserList(selUser);
        List<AppletClerkUser> fList = selList.stream().filter(it -> !it.getId().equals(clerkUser.getId())).collect(Collectors.toList());
        if(fList.size() > 0) {
            throw new RuntimeException("手机号重复！");
        }
        if (StringUtils.isEmpty(clerkUser.getStoreId())) {
            throw new RuntimeException("所属门店不能为空！");
        }
    }
}
